using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using ZedGraph;

namespace ICP_MS.Plugin.LA300;

internal class CustomGraph : ZedGraphControl
{
	private List<IntegerRegions> _regions = new List<IntegerRegions>();

	public void SetRegions(IntegerRegions[] regions)
	{
		_regions.Clear();
		if (regions != null)
		{
			_regions.AddRange(regions);
		}
	}

	protected override void OnPaint(PaintEventArgs e)
	{
		base.OnPaint(e);
		foreach (GraphPane pane in base.MasterPane.PaneList)
		{
			List<RectangleF> rects = new List<RectangleF>();
			foreach (IntegerRegions region in _regions)
			{
				PointF pt1 = pane.GeneralTransform(Math.Max(region.Start, pane.XAxis.Scale.Min), pane.YAxis.Scale.Max, CoordType.AxisXYScale);
				PointF pt2 = pane.GeneralTransform(Math.Min(region.End, pane.XAxis.Scale.Max), Math.Max(0.0, pane.YAxis.Scale.Min), CoordType.AxisXYScale);
				if (pt1.X <= pt2.X)
				{
					rects.Add(new RectangleF(pt1.X, pt1.Y + 30f, pt2.X - pt1.X, pt2.Y - pt1.Y - 30f));
				}
			}
			if (rects.Count > 0)
			{
				SolidBrush brush = new SolidBrush(Color.FromArgb(50, Color.Yellow));
				Pen pen = new Pen(Color.Red);
				e.Graphics.FillRectangles(brush, rects.ToArray());
				e.Graphics.DrawRectangles(new Pen(Color.Red), rects.ToArray());
				brush.Dispose();
				pen.Dispose();
			}
		}
	}
}
